package com.freecloud.javabasics.thread.state;

import com.freecloud.common.LoggerUtil;

import java.util.concurrent.TimeUnit;

/**
 * 中断线程
 * @Author: maomao
 * @Date: 2020-01-21 16:55
 */
public class InterruptDemo {

    private static int i;

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(()->{
            while (!Thread.currentThread().isInterrupted()){
                i++;
            }
            System.out.println("i : " + i);
        });
        thread.start();
        LoggerUtil.printThread("1 isInterrupted ：" + thread.isInterrupted());

        LoggerUtil.printThread("1 state ：" + thread.getState());

        TimeUnit.SECONDS.sleep(1);

        thread.interrupt();

        LoggerUtil.printThread("2 isInterrupted ：" + thread.isInterrupted());

        LoggerUtil.printThread("2 state ：" + thread.getState());

        thread.join();

        LoggerUtil.printThread("3 isInterrupted ：" + thread.isInterrupted());

        LoggerUtil.printThread("3 state ：" + thread.getState());
    }
}
